//
//  UserService.m
//  CC
//
//  Created by Calvin.wang on 3/25/15.
//  Copyright (c) 2015 Calvin. All rights reserved.
//

#import "UserService.h"
#import "SQLiteHelper.h"

@interface UserService()
@property SQLiteHelper *sqliteHelper;
@end

@implementation UserService
- (instancetype)init
{
    self = [super init];
    if (self) {
        _sqliteHelper=[[SQLiteHelper alloc] init];
    }
    return self;
}

-(void) addUser:(User *)user{
    NSString *sql=[NSString stringWithFormat:@"INSERT INTO User (name,screenName, profileImageUrl,userType,city) VALUES('%@','%@','%@','%@','%@')",user.name,user.screenName,user.profileImageUrl,user.userType,user.city];
    [_sqliteHelper executeNonQuery:sql];
}

-(void) removeUser:(User *)user{
    NSString *sql =[NSString stringWithFormat:@"DELETE FROM User WHERE Id='%@'",user.Id];
    [_sqliteHelper executeNonQuery:sql];
}

-(void) removeUserByName:(NSString *)name{
    NSString *sql =[NSString stringWithFormat:@"DELETE FROM User WHERE name='%@'",name];
    [_sqliteHelper executeNonQuery:sql];
}

-(void) modifyUser:(User *)user{
    NSString *sql=[NSString stringWithFormat:@"UPDATE User SET name='%@',screenName='%@',profileImageUrl='%@',userType='%@',city='%@' WHERE Id='%@'",user.name,user.screenName,user.profileImageUrl,user.userType,user.city];
    [_sqliteHelper executeNonQuery:sql];
}

-(User *) getUserById:(int)Id{
    User *user=[[User alloc] init];
    NSString *sql=[NSString stringWithFormat:@"SELECT name,screenName,profileImageUrl,userType,city FROM User WHERE Id='%i'",Id];
    NSArray *rows= [_sqliteHelper executeQuery:sql];
    if (rows&&rows.count>0) {
        [user setValuesForKeysWithDictionary:rows[0]];
    }
    return user;
}

-(User *) getUserByName:(NSString *)name{
    User *user=[[User alloc] init];
    NSString *sql=[NSString stringWithFormat:@"SELECT Id, name,screenName,profileImageUrl,userType,city FROM User WHERE name='%@'",name];
    NSArray *rows=[_sqliteHelper executeQuery:sql];
    if (rows&&rows.count>0) {
        [user setValuesForKeysWithDictionary:rows[0]];
    }
    
    return user;
}


@end
